package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.dao.MchntBaseInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.MchntBaseInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.MchntBaseInfo;
import cc.rengu.igas.bomp.common.entity.WechatMchntInfo;
import cc.rengu.igas.bomp.common.enums.MchntStatusEnum;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.model.WechatIntegration;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.request.WechatRealNameApplyRequest;
import cc.rengu.igas.bomp.facade.response.WechatRealNameApplyResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;

/**
 * @version 1.0
 * @author: wukf
 * @Date: 2020/4/9 14:59
 * @Description: 微信商户实名认证申请
 * <p>
 * 实名认证业务规则与曾子龙确认：
 * 1、允许实名认证状态为[未实名认证]进行实名认证操作
 * 2、若微信实名认证通过，则不允许修改法人信息，证件信息等数据
 * <p>
 * 处理流程与曾子龙确认：
 * 1、校验商户基础信息表商户状态为正式商户
 * 2、校验微信商户信息表（微信入驻时初始化该表数据）若为空或微信商户号为空，则返回失败[必须先进行微信商户入驻，才能进行实名认证操作]
 * 3、若微信实名认证状态为已实名认证，则直接返回
 * 4、向微信查询获取商户开户意愿确认状态（通过微信商户号查询），判断状态
 * 4.1 若微信返回微信已授权，则更新微信表的实名认证状态为已实名认证（申请编号为空），结束流程
 * 4.2 若返回其他状态，则继续执行
 * 5、组装参数，更新数据库微信商户表实名认证申请编号（sysSeqNum+序号），调用微信通道的实名验证提交申请接口，判断返回结果
 * 5.1 若微信返回申请单号则更新微信商户表的实名认证申请编号；
 * 5.2 若没有则透传微信返回的错误信息
 */
public class WechatRealNameApplyService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        WechatRealNameApplyRequest request = new WechatRealNameApplyRequest();
        ConvertUtil.convertOutput(request);
        return request;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        WechatRealNameApplyRequest wechatRealNameApplyRequest = (WechatRealNameApplyRequest) request;
        WechatRealNameApplyResponse wechatRealNameApplyResponse = new WechatRealNameApplyResponse();
        BizResponse<WechatRealNameApplyResponse> bizResponse = new BizResponse<>();
        bizResponse.setResult(wechatRealNameApplyResponse);
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        wechatRealNameApplyResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        wechatRealNameApplyResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

        MchntBaseInfoMapper mchntBaseInfoMapper = new MchntBaseInfoMapperImpl();
        MchntBaseInfo mchntBaseInfo = mchntBaseInfoMapper.selectMchntBaseInfoByPrimaryKey(wechatRealNameApplyRequest.getHeader().getInstId(), wechatRealNameApplyRequest.getMchntNo());
        if (null == mchntBaseInfo) {
            rglog.error("微信商户实名认证申请失败, 根据商户号查询商户基础信息为空，instId:{},mchntNo:{}",
                    wechatRealNameApplyRequest.getHeader().getInstId(), wechatRealNameApplyRequest.getMchntNo());
            throw new BizException(RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespCode(), RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespDesc());
        }
        if (!MchntStatusEnum.NORMAL.getStatus().equals(mchntBaseInfo.getMchntStatus())) {
            rglog.error("商户状态不合法,status:<{}>", mchntBaseInfo.getMchntStatus());
            throw new BizException(RespCodeEnum.MCHNT_STATUS_ABNORMAL.getRespCode(), RespCodeEnum.MCHNT_STATUS_ABNORMAL.getRespDesc());
        }

        WechatIntegration wechatIntegration = new WechatIntegration();
        wechatIntegration.beforeRealNameCheck(mchntBaseInfo);

        WechatMchntInfo wechatMchntInfo = wechatIntegration.realNameApplyment(mchntBaseInfo);

        wechatRealNameApplyResponse.setRealNameFlag(wechatMchntInfo.getRealNameFlag());
        wechatRealNameApplyResponse.setRealNameApplyNo(wechatMchntInfo.getRealNameApplyNo());
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        WechatRealNameApplyResponse wechatRealNameApplyResponse = (WechatRealNameApplyResponse) bizResponse.getResult();
        ConvertUtil.convertInput(wechatRealNameApplyResponse);
    }

}
